package com.xckj.zuoliao.presenter;

import androidx.annotation.NonNull;

import com.xckj.zuoliao.bean.LoginBean;
import com.xckj.zuoliao.framework.BaseBean;
import com.xckj.zuoliao.framework.net.RetrofitManager;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.ILoginView;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: 代码的搬运工
 * @Describe: 登录
 * @Date: 2024/3/27 23:41
 */
public class LoginPresenter {
    private final ILoginView loginView;

    public LoginPresenter(ILoginView loginView) {
        this.loginView = loginView;
    }

    /**
     * @param regist_code 注册码
     * @param mobile_id 设备id标识
     */
    public void login(String regist_code, String mobile_id) {
        loginView.showLoading();
        Call<BaseBean<LoginBean>> call = RetrofitManager.getInstance().getService().login(regist_code, mobile_id);
        call.enqueue(new Callback<BaseBean<LoginBean>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<LoginBean>> call, @NonNull Response<BaseBean<LoginBean>> response) {
                if (response.isSuccessful()) {
                    BaseBean<LoginBean> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        loginView.loginSuccess(baseBean.data);
                    }else {
                        loginView.loginFailed(baseBean.msg);
                    }
                }else {
                    loginView.loginFailed("请求失败");
                }
                loginView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<LoginBean>> call, @NonNull Throwable t) {

            }
        });
    }
}
